/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MessageIO;

import java.io.IOException;
import java.net.Socket;
import java.util.Formatter;

/**
 * Sends a message to the chat server in a separate runnable
 * @author Mark
 */
public class MessageSender implements Runnable
    {
    private Socket client;//socket to send message
    private String message;//message data

    /**
     * Creates a new MessageSender
     * @param clientSocket socket to send message
     * @param userName name of sender
     * @param message message data to be sent
     */
    public MessageSender (Socket clientSocket, String userName, String message)
        {
        super();
        client = clientSocket;//stores clientSocket
        //Assembles message
        this.message = userName + SocketConstants.MESSAGE_SEPERATOR + message;
        }

    /**
     * Sends message and ends
     */
    public void run()
        {
        try//send message and flush PrintWriter
            {
            Formatter out = new Formatter(client.getOutputStream());
            out.format("%s\n", message);//send
            out.flush();//flush
            }
        catch (IOException io)
            {
            io.printStackTrace();
            }
        }
    }
